#ifndef __OJ_VIEW_HPP__
#define __OJ_VIEW_HPP__

#include <iostream>
#include <string>
#include <ctemplate/template.h>
// #include "./oj_model.hpp"
#include "./oj_model2.hpp"

namespace ns_view
{
    const std::string template_path="./template_html/";

    class View
    {
    public:
        View()=default;
        ~View()=default;

        void AllExpandHtml(const std::vector<ns_model::Question>& questions,std::string* html)
        {
            /*1.形成路径*/
            std::string src_html=template_path+"all_questions.html";

            /*2.形成数字典*/
            ctemplate::TemplateDictionary root("all_questions");
            for(auto& q:questions)
            {
                ctemplate::TemplateDictionary* sub=root.AddSectionDictionary("question_list");
                sub->SetValue("number",q._number);
                sub->SetValue("title",q._title);
                sub->SetValue("star",q._star);
            }

            /*3.获取被渲染的html*/
            ctemplate::Template* tpl=ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);

            /*4.开始完成渲染功能*/
            tpl->Expand(html,&root);
        }

        void OneExpandHtml(const ns_model::Question& question,std::string* html)
        {
            /*1.形成路径*/
            std::string src_html=template_path+"one_question.html";

            /*2.形成数字典*/
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number",question._number);
            root.SetValue("title",question._title);
            root.SetValue("star",question._star);
            root.SetValue("desc",question._desc);
            root.SetValue("pre_code",question._header);

            /*3.获取被渲染的html*/
            ctemplate::Template* tpl=ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);

            /*4.开始完成渲染工作*/
            tpl->Expand(html,&root);
        }
    };
}

#endif